﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class RPhieuNhapBO : Csla.BusinessBase<RPhieuNhapBO>
    {
        #region Business Properties

        private int? _RPhieuNhapID = null;
        public int? RPhieuNhapID
        {
            get { return this._RPhieuNhapID; }
            set { this._RPhieuNhapID = value; PropertyHasChanged("RPhieuNhapID"); }
        }

        private string _MaSo = "0";
        public string MaSo
        {
            get { return this._MaSo; }
            set { this._MaSo = value; PropertyHasChanged("MaSo"); }
        }

        private string _ChungTu = null;
        public string ChungTu
        {
            get { return this._ChungTu; }
            set { this._ChungTu = value; PropertyHasChanged("ChungTu"); }
        }

        private string _KhachHangID = null;
        public string KhachHangID
        {
            get { return this._KhachHangID; }
            set { this._KhachHangID = value; PropertyHasChanged("KhachHangID"); }
        }

        private string _DiaChi = null;
        public string DiaChi
        {
            get { return this._DiaChi; }
            set { this._DiaChi = value; PropertyHasChanged("DiaChi"); }
        }

        private string _TaiKhoan = null;
        public string TaiKhoan
        {
            get { return this._TaiKhoan; }
            set { this._TaiKhoan = value; PropertyHasChanged("TaiKhoan"); }
        }

        private DateTime? _NgayLapHoaDon = null;
        public DateTime? NgayLapHoaDon
        {
            get { return this._NgayLapHoaDon; }
            set { this._NgayLapHoaDon = value; PropertyHasChanged("NgayLapHoaDon"); }
        }

        private string _NoiDung = null;
        public string NoiDung
        {
            get { return this._NoiDung; }
            set { this._NoiDung = value; PropertyHasChanged("NoiDung"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private string _NguoiLapHoaDonID = null;
        public string NguoiLapHoaDonID
        {
            get { return this._NguoiLapHoaDonID; }
            set { this._NguoiLapHoaDonID = value; PropertyHasChanged("NguoiLapHoaDonID"); }
        }

        private string _KeToanID = null;
        public string KeToanID
        {
            get { return this._KeToanID; }
            set { this._KeToanID = value; PropertyHasChanged("KeToanID"); }
        }

        private string _GiamDocID = null;
        public string GiamDocID
        {
            get { return this._GiamDocID; }
            set { this._GiamDocID = value; PropertyHasChanged("GiamDocID"); }
        }

        private string _NguoiNhanHangID = null;
        public string NguoiNhanHangID
        {
            get { return this._NguoiNhanHangID; }
            set { this._NguoiNhanHangID = value; PropertyHasChanged("NguoiNhanHangID"); }
        }

        private string _ThuKhoID = null;
        public string ThuKhoID
        {
            get { return this._ThuKhoID; }
            set { this._ThuKhoID = value; PropertyHasChanged("ThuKhoID"); }
        }

        private string _NguoiGiaoHang = null;
        public string NguoiGiaoHang
        {
            get { return this._NguoiGiaoHang; }
            set { this._NguoiGiaoHang = value; PropertyHasChanged("NguoiGiaoHang"); }
        }

        private double? _TongSo = null;
        public double? TongSo
        {
            get { return this._TongSo; }
            set { this._TongSo = value; PropertyHasChanged("TongSo"); }
        }

        private double? _ThanhTien = null;
        public double? ThanhTien
        {
            get { return this._ThanhTien; }
            set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
        }

        private double? _TienPhaiTra = null;
        public double? TienPhaiTra
        {
            get { return this._TienPhaiTra; }
            set { this._TienPhaiTra = value; PropertyHasChanged("TienPhaiTra"); }
        }

        private string _BangChu = null;
        public string BangChu
        {
            get { return this._BangChu; }
            set { this._BangChu = value; PropertyHasChanged("BangChu"); }
        }

        private double? _TienTra = null;
        public double? TienTra
        {
            get { return this._TienTra; }
            set { this._TienTra = value; PropertyHasChanged("TienTra"); }
        }

        private double? _TienThieu = null;
        public double? TienThieu
        {
            get { return this._TienThieu; }
            set { this._TienThieu = value; PropertyHasChanged("TienThieu"); }
        }

        private double? _TienPhatSinh = null;
        public double? TienPhatSinh
        {
            get { return this._TienPhatSinh; }
            set { this._TienPhatSinh = value; PropertyHasChanged("TienPhatSinh"); }
        }

        private string _LyDoPhatSinh = null;
        public string LyDoPhatSinh
        {
            get { return this._LyDoPhatSinh; }
            set { this._LyDoPhatSinh = value; PropertyHasChanged("LyDoPhatSinh"); }
        }

        private double? _MucVAT = null;
        public double? MucVAT
        {
            get { return this._MucVAT; }
            set { this._MucVAT = value; PropertyHasChanged("MucVAT"); }
        }

        private double? _TienVAT = null;
        public double? TienVAT
        {
            get { return this._TienVAT; }
            set { this._TienVAT = value; PropertyHasChanged("TienVAT"); }
        }

        private double? _MucGiamGia = null;
        public double? MucGiamGia
        {
            get { return this._MucGiamGia; }
            set { this._MucGiamGia = value; PropertyHasChanged("MucGiamGia"); }
        }

        private double? _TienGiamGia = null;
        public double? TienGiamGia
        {
            get { return this._TienGiamGia; }
            set { this._TienGiamGia = value; PropertyHasChanged("TienGiamGia"); }
        }

        private double? _MucPhucVu = null;
        public double? MucPhucVu
        {
            get { return this._MucPhucVu; }
            set { this._MucPhucVu = value; PropertyHasChanged("MucPhucVu"); }
        }

        private double? _TienPhucVu = null;
        public double? TienPhucVu
        {
            get { return this._TienPhucVu; }
            set { this._TienPhucVu = value; PropertyHasChanged("TienPhucVu"); }
        }

        private double? _MucChietKhau = null;
        public double? MucChietKhau
        {
            get { return this._MucChietKhau; }
            set { this._MucChietKhau = value; PropertyHasChanged("MucChietKhau"); }
        }

        private double? _TienChietKhau = null;
        public double? TienChietKhau
        {
            get { return this._TienChietKhau; }
            set { this._TienChietKhau = value; PropertyHasChanged("TienChietKhau"); }
        }

        private int? _ThanhToan = null;
        public int? ThanhToan
        {
            get { return this._ThanhToan; }
            set { this._ThanhToan = value; PropertyHasChanged("ThanhToan"); }
        }

        private string _TinhTrangThanhToan = null;
        public string TinhTrangThanhToan
        {
            get { return this._TinhTrangThanhToan; }
            set { this._TinhTrangThanhToan = value; PropertyHasChanged("TinhTrangThanhToan"); }
        }

        private string _HinhThucThanhToanID = null;
        public string HinhThucThanhToanID
        {
            get { return this._HinhThucThanhToanID; }
            set { this._HinhThucThanhToanID = value; PropertyHasChanged("HinhThucThanhToanID"); }
        }

        private int? _TinhTrangXuatKho = null;
        public int? TinhTrangXuatKho
        {
            get { return this._TinhTrangXuatKho; }
            set { this._TinhTrangXuatKho = value; PropertyHasChanged("TinhTrangXuatKho"); }
        }

        private string _USRC = null;
        public string USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _RPhieuNhapID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newRPhieuNhapID = -1;
        private RPhieuNhapBO()
        {
            _RPhieuNhapID = newRPhieuNhapID--;
        }

        public RPhieuNhapBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public RPhieuNhapBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static RPhieuNhapBO NewRootRPhieuNhapBO()
        {
            return DataPortal.Create<RPhieuNhapBO>(new RootCriteria());
        }

        internal static RPhieuNhapBO NewChildRPhieuNhapBO()
        {
            return DataPortal.Create<RPhieuNhapBO>(new ChildCriteria());
        }

        public static RPhieuNhapBO GetRootRPhieuNhapBO(int _RPhieuNhapID)
        {
            return DataPortal.Fetch<RPhieuNhapBO>(new RootCriteria(_RPhieuNhapID));
        }

        public static RPhieuNhapBO GetRootRPhieuNhapBOByWhere(string where)
        {
            return DataPortal.Fetch<RPhieuNhapBO>(where);
        }

        internal static RPhieuNhapBO GetChildRPhieuNhapBO(SqlDataReader dataReader)
        {
            return new RPhieuNhapBO(dataReader);
        }

        public static void DeleteRPhieuNhapBO(int _RPhieuNhapID)
        {
            DataPortal.Delete(new RootCriteria(_RPhieuNhapID));
        }

        public static void DeleteRPhieuNhapBO(int _RPhieuNhapID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_RPhieuNhapID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int RPhieuNhapID;
            public bool directDelete = false;
            public RootCriteria(int _RPhieuNhapID)
            {
                this.RPhieuNhapID = _RPhieuNhapID;
                this.directDelete = false;
            }

            public RootCriteria(int _RPhieuNhapID, bool directDelete)
            {
                this.RPhieuNhapID = _RPhieuNhapID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int RPhieuNhapID;
            public bool directDelete = false;
            public ChildCriteria(int _RPhieuNhapID)
            {
                this.RPhieuNhapID = _RPhieuNhapID;
                this.directDelete = false;
            }

            public ChildCriteria(int _RPhieuNhapID, bool directDelete)
            {
                this.RPhieuNhapID = _RPhieuNhapID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {

        }

        private void This_Fetch(string where)
        {

        }

        private void Fetch(SqlDataReader dataReader)
        {

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {

        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {

        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_RPhieuNhapID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method


        public void CopyFromRPhieuNhapBO(PhieuNhapBO hdbBO)
        {
            this.RPhieuNhapID = hdbBO.PhieuNhapID;
            MaSo = hdbBO.MaSo;
            ChungTu = hdbBO.ChungTu;
            NgayLapHoaDon = hdbBO.NgayLapHoaDon;
            NguoiGiaoHang = hdbBO.NguoiGiaoHang;
            LyDoPhatSinh = hdbBO.LyDoPhatSinh;

            if (StaticUtil.GetInt(hdbBO.NhaCungCapID.Value, -1) < 0)
                KhachHangID = "";
            else
            {
                KhachHangBO khBO = KhachHangBO.GetRootKhachHangBO(StaticUtil.GetInt(hdbBO.NhaCungCapID.Value, -1));
                if (StaticUtil.GetInt(khBO.KhachHangID.Value, -1) >= 0)
                {
                    KhachHangID = khBO.Ten;
                    DiaChi = khBO.Diachi + " - Điện thoại: " + khBO.DienThoai + " - Fax: " + khBO.Fax;
                    TaiKhoan = khBO.SoTaiKhoan + " - Tại Ngân Hàng: " + khBO.TaiNganHang;
                }
            }

            if (hdbBO.ThanhToan.Value == 0)
                HinhThucThanhToanID = "";
            else
            {
                DanhMucBO dmHinhThucThanhToan = DanhMucBO.GetRootDanhMucBO(StaticUtil.GetInt(hdbBO.HinhThucThanhToanID.Value, -1));
                if (StaticUtil.GetInt(dmHinhThucThanhToan.DanhMucID.Value, -1) >= 0)
                {
                    HinhThucThanhToanID = dmHinhThucThanhToan.NoiDung;
                }
            }

            NoiDung = hdbBO.NoiDung;
            if (StaticUtil.GetDouble(hdbBO.TienThieu.Value, -1) < 0)
                GhiChu = "Còn công nợ: " + StaticUtil.GetDouble(hdbBO.TienThieu.Value, -1).ToString().Remove(0, 1);
            else
                GhiChu = "Đã thanh toán đủ";

            ThanhTien = StaticUtil.GetDouble(hdbBO.ThanhTien.Value, 0);
            TongSo = StaticUtil.GetDouble(hdbBO.TongSo.Value, 0);
            TienPhatSinh = StaticUtil.GetDouble(hdbBO.TienPhatSinh.Value, 0);

            MucVAT = StaticUtil.GetDouble(hdbBO.MucVAT.Value, 0);
            TienVAT = MucVAT * ThanhTien;
            MucGiamGia = StaticUtil.GetDouble(hdbBO.MucGiamGia.Value, 0);
            TienGiamGia = MucGiamGia * ThanhTien;
            MucChietKhau = StaticUtil.GetDouble(hdbBO.MucChietKhau.Value, 0);
            TienChietKhau = MucChietKhau * ThanhTien;
            MucPhucVu = StaticUtil.GetDouble(hdbBO.MucPhucVu.Value, 0);
            TienPhucVu = MucPhucVu * ThanhTien;
            TienPhaiTra = StaticUtil.GetDouble(hdbBO.TienPhaiTra.Value, 0);
            TienTra = StaticUtil.GetDouble(hdbBO.TienTra.Value, 0);
            TienThieu = StaticUtil.GetDouble(hdbBO.TienThieu.Value, 0);
            BangChu = hdbBO.BangChu;


            NhanVienBOList nhanvienBOList = NhanVienBOList.GetNhanVienBOList();
            int? _value = null;
            _value = StaticUtil.GetRealInt(hdbBO.NguoiNhanHangID);
            if (_value != null)
                if (nhanvienBOList.ContainsKey(_value))
                    NguoiNhanHangID = nhanvienBOList.Value(_value);
                else
                    NguoiNhanHangID = null;

            _value = StaticUtil.GetRealInt(hdbBO.ThuKhoID);
            if (_value != null)
                if (nhanvienBOList.ContainsKey(_value))
                    ThuKhoID = nhanvienBOList.Value(_value);
                else
                    ThuKhoID = null;

            _value = StaticUtil.GetRealInt(hdbBO.KeToanID);
            if (_value != null)
                if (nhanvienBOList.ContainsKey(_value))
                    KeToanID = nhanvienBOList.Value(_value);
                else
                    KeToanID = null;

            _value = StaticUtil.GetRealInt(hdbBO.GiamDocID);
            if (_value != null)
                if (nhanvienBOList.ContainsKey(_value))
                    GiamDocID = nhanvienBOList.Value(_value);
                else
                    GiamDocID = null;

            _value = StaticUtil.GetRealInt(hdbBO.USRC);
            if (_value != null)
                if (nhanvienBOList.ContainsKey(_value))
                    USRC = nhanvienBOList.Value(_value);
                else
                    USRC = null;


        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_RPhieuNhapID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
